package auctionhaus

class Customer {

	transient springSecurityService

    String email
	String password
	boolean enabled
	boolean accountExpired
	boolean accountLocked
	boolean passwordExpired
    Date dateCreated

    static hasMany = [listings: Listing]
    static mappedBy = [listings: "seller"]

    static constraints = {
        dateCreated nullable: true
        password blank: false, minSize: 6
        email email: true, unique: true, blank:  false

    }

	static mapping = {
		password column: '`password`'
	}

	Set<Role> getAuthorities() {
		CustomerRole.findAllByCustomer(this).collect { it.role } as Set
	}

	def beforeInsert() {

		encodePassword()
	}

	def beforeUpdate() {
		if (isDirty('password')) {
			encodePassword()
		}
	}

	protected void encodePassword() {

        if(springSecurityService)
		    password = springSecurityService.encodePassword(password)
	}
}
